<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getEmployees">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="40">
			</el-table-column>
			<el-table-column prop="username" label="姓名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮箱" width="140"  sortable>
			</el-table-column>
			<el-table-column prop="phone" label="电话" width="140"  sortable>
			</el-table-column>
			<el-table-column prop="salt" label="密码盐" width="200"  sortable>
			</el-table-column>
			<el-table-column prop="password" label="密码" width="200"  sortable>
			</el-table-column>
			<el-table-column prop="age" label="年龄" width="100"  sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100" :formatter="formatState" sortable>
			</el-table-column>
			<el-table-column prop="department.name" label="所属部门" width="100" sortable>
			</el-table-column>
			<el-table-column prop="shop.name" label="店铺名" width="100" sortable>
			</el-table-column>
			<el-table-column label="操作" width="160">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="姓名" prop="username">
					<el-input v-model="editForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="editForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话" prop="phone">
					<el-input v-model="editForm.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="editForm.password" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="年龄" prop="age">
					<el-input v-model="editForm.age" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="状态">
					<el-radio-group v-model="editForm.state">
						<el-radio class="radio" :label="1">启用</el-radio>
						<el-radio class="radio" :label="0">禁用</el-radio>
					</el-radio-group>
				</el-form-item>

        所属部门:
        <el-select v-model="editForm.department" value-key="id"  placeholder="请选择">
          <el-option
              v-for="dept in departments"
              :label="dept.name"
              :value="dept">
            <span style="float: left">{{ dept.name }}</span>
            <span style="float: right; font-size: 13px">{{ dept.sn }}</span>
          </el-option>
        </el-select><br>

        店铺名:
        <el-select v-model="editForm.shop" value-key="id"  placeholder="请选择">
          <el-option
              v-for="shop in shops"
              :label="shop.name"
              :value="shop">
            <span style="float: left">{{ shop.name }}</span>
            <span style="float: right; font-size: 13px">{{ shop.tel }}</span>
          </el-option>
        </el-select>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, addEmployee } from '../../api/api';

	export default {
		data() {
			return {
        title:'',
				filters: {
					name: ''
				},
				employees: [],
        departments:[],
        shops:[],
				total: 0,
				currentPage: 1,
        pageSize:6,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					username: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
          username:'',
          email:'',
          phone:'',
          salt:'',
          password:'',
          age:0,
          state:0,
          department:{},
          shop:{},
				},
				addLoading: false,
			}
		},
		methods: {
			//性别显示转换
			formatState: function (row, column) {
				return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getEmployees();
			},
      //获取部门数据
      getDeptsOfEmps(){
        this.$http.get("/employee/deptsOfEmps")
          .then(res=>{
            this.departments = res.data
            // console.log("deptsOfEmps====",res.data)
          })
          .catch(error => {
            console.error("error==",error)
          })
      },
      //获取店铺数据
      getShopsOfEmps(){
        this.$http.get("/employee/shopsOfEmps")
          .then(res=>{
            this.shops = res.data;
            // console.log("shopsOfEmps====",res.data)
          })
          .catch(error => {
            console.error("error==",error)
          })
      },

			//获取用户列表
			getEmployees() {
				let para = {
					currentPage: this.currentPage,
					pageSize:this.pageSize,
          name:this.filters.name
				};
				this.listLoading = true;
				//NProgress.start();
        this.$http.post("/employee",para)
            .then((res) => {
					this.total = res.data.total;
          let data = res.data.data
              for (let i in data) {
                for (let j in data[i]) {
                    if(!data[i][j]){
                      data[i][j] = ''
                    }
                }
              }
          this.employees = data
					this.listLoading = false;
					//NProgress.done();
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					// let para = { id: row.id };
					// removeEmployee(para)
          this.$http.delete("/employee/"+row.id)
              .then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getEmployees();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title="编辑";
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
        this.getDeptsOfEmps();
        this.getShopsOfEmps();
			},
			//显示新增界面
			handleAdd: function () {
        this.title = "新增";
				this.editFormVisible = true;
				this.editForm = {
          username:'',
          email:'',
          phone:'',
          salt:'',
          password:'',
          age:0,
          state:1,
          department:{},
          shop:{},
				};
        this.getDeptsOfEmps();
        this.getShopsOfEmps();
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
              para.department = {"id":this.editForm.department.id}
              para.shop = {"id":this.editForm.shop.id}
							this.$http.put("/employee",para)
                  .then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getEmployees();
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					// batchRemoveEmployee(para)
          this.$http.patch("/employee/delete",ids)
              .then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getEmployees();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getEmployees();
		}
	}

</script>

<style scoped>

</style>